home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
scope
/
101-125
/
scopedisk122
/
3d
/
test.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-19
|
6KB
|
182 lines
/* Test Demo Program - By Steve Ludtke */
/* cre : 1/3/90 mod : 3/11/90 */
/* Copyright 1990 */
/*
The 3d library and all associated software in this distribution
is Copyright 1990 by Steven J. Ludtke. You have permission to
use and/or modify this code for any purpose commercial or non-
commercial with two conditions : I must be given credit in any
distributed product's documentation, and if any part of this
package is used in any commercial product (even Shareware) one
free copy of said software must be sent to me at the following
address : Steven Ludtke, 406 Yale Cir., Glenwood Springs, CO
81601; all other royalties are waived. This Copyright notice
must accompany any distributions of any part of this package,
and in general, the package should be distributed intact, with
no modifications. This notice must not be removed from the 3d.c,
test.c, and cube.c source code in this release. Modified version
may not be distributed without permission. If the conditions
in the disclaimer are not valid in your home state, permission
to use this software is revoked.
*/
/*
INSTRUCTIONS : This program will display shapes stored in a standard file
format from any perspective. It does not use double buffered animation. The
user changes his viewpoint using the keys on the numeric keypad as is shown
in the code. Also, the '5' key toggles filling on and off, and 'q' exits
the program. Console i/o is used, so you have to press return before
anything will take effect. Run the program as follows :
'test <file.3d> <scale, usu 1>'
The 3d file format is :
number of vertices (n)
x0,y0,z0
x1,y1,z1
.
.
.
xn,yn,zn
number of line array elements (m)
a0 b0
a1 b1
.
.
.
am bm
<EOF>
where a are vertex numbers or colors, and b is : <0> - continue shape/line,
<1> - begin new line, <2> - change fill color, <3> - change outline color.
look at the color changing warnings in the docs.
*/
#include<stdio.h>
#define D3VDIST 256 /* distance to vanishing point */
#define REZ 512 /* calc resolution must be 2^x */
#define XCEN 250 /* center of bitmap X */
#define YCEN 80 /* center of bitmap Y */
#define XHI 600 /* x,y limits for clipping */
#define YHI 165
#define XLO 0
#define YLO 0
#define ASPECT 23/10 /* aspect ratio */
#include "3d.c"
APTR IntuitionBase,GfxBase;
struct NewWindow NW = { 0,1,600,180,1,1,0,
WINDOWDEPTH|WINDOWDRAG|GIMMEZEROZERO|SMART_REFRESH|NOCAREREFRESH,
NULL,NULL,(UBYTE *)"3D test window",NULL,NULL,0,0,0,0,WBENCHSCREEN };
long *xxx; /* points to x vertex data */
long *yyy; /* ... */
long *zzz;
LINES *line; /* points to beginning of LINES array */
main(argc,argv)
int argc;
char *argv[];
{
VECTOR v;
FILE *in;
double a1,a2,a3;
char c;
int i,p,m,t,nl,nv,sca,f,x,z,dx,dy,dz;
struct Window *win;
GfxBase=(APTR) OpenLibrary("graphics.library",0);
IntuitionBase=(APTR) OpenLibrary("intuition.library",0);
f=0;
if (argc!=3) { printf("test <file> <scale>\n"); exit(0); }
in=fopen(argv[1],"r"); /* read 3d data */
if (in==NULL) { printf("fnf\n"); exit(0); }
sscanf(argv[2]," %d",&sca);
fscanf(in," %d",&nv);
xxx=(long *)malloc(nv*sizeof(long));
yyy=(long *)malloc(nv*sizeof(long));
zzz=(long *)malloc(nv*sizeof(long));
if (xxx==NULL || yyy==NULL || zzz==NULL) { printf("Out of memory\n"); exit(0); }
for (p=0; p<nv; p++) fscanf(in," %ld , %ld , %ld",&xxx[p],&yyy[p],&zzz[p]);
fscanf(in," %d",&nl);
line=(LINES *)malloc(nl*sizeof(LINES));
if (line==NULL) { printf("Out of memory\n"); exit(0); }
for (p=0; p<nl; p++) { fscanf(in," %d %d",&t,&m);
line[p].l=t;
if (m==1) line[p].nl=1; else line[p].nl=0;
if (m==2) line[p].nc=1; else line[p].nc=0;
if (m==3) line[p].nco=1; else line[p].nco=0; }
win=(struct Window *)OpenWindow(&NW);
/* open a window and prepare for d3surf() */
Init3Ras(win->RPort,NULL);
if (win==NULL) { printf("Window err\n"); exit(1); }
v.x=xxx; /* set up VECTOR structure */
v.y=yyy;
v.z=zzz;
v.tx=(long *) malloc(nv*sizeof(long));
v.ty=(long *) malloc(nv*sizeof(long));
v.tz=(long *) malloc(nv*sizeof(long));
a1=a2=a3=0;
x=z=dx=dz=p=0;
p=100;
dy=1;
SetAPen(win->RPort,1); /* set pens, in case no color data in file */
SetOPen(win->RPort,2);
SetDrMd(win->RPort,JAM1);
while ((c=getchar())!='q') { /* read from keyboard */
switch(c) {
case '9': a1+=PI/20; break; /* rotates with setxfm() */
case '7': a1-=PI/20; break;
case '6': a2+=PI/20; break;
case '4': a2-=PI/20; break;
case '3': a3+=PI/20; break;
case '1': a3-=PI/20; break;
case '8': p+=100; break; /* used with setxfm & setxfm2 */
case '2': p-=100; break;
case '5': f^=1; break; /* toggle line/surf mode */
case 'i': z-=100; break; /* used with setxfm2 */
case 'm': z+=100; break;
case 'j': x-=100; break;
case 'k': x+=100; break;
case 'w': dz-=1; break;
case 'z': dz+=1; break;
case 'a': dx-=1; break;
case 's': dx+=1; break;
case 'e': dy+=1; break;
case 'x': dy-=1; break;
}
if (c!=10) {
setxfm(a1,a2,a3,0,p,0,sca,1); /* set new rotation matrix */
/* setxfm2(dx,dy,dz,x,p,z,sca,1); /* set new rotation matrix */
/* rotatevp(&v,nv); /* use with setxfm2, rotate with perspective */
rotatev(&v,nv); /* rotate with perspective */
SetRast(win->RPort,0); /* clear window */
if (f==0) d3lines(&v,line,nl,win->RPort); /* draw lines or filled shapes */
else d3surf(&v,line,nl,win->RPort);
}
}
Exit3d(win->RPort); /* free up scratch memory */
CloseWindow(win);
free(v.tx);
free(v.ty);
free(v.tz);
free(xxx);
free(yyy);
free(zzz);
free(line);
}